use std::str;
use std::vec::Vec;

use winapi::shared::minwindef::DWORD;
use winapi::um::winbase::GetLogicalDriveStringsA;
use winapi::um::winnt::LPSTR;

pub fn root_path() -> Vec<String> {
    let mut buffer = vec![0u8; 1024];
    let buffer_ptr = buffer.as_mut_ptr() as LPSTR;
    let buffer_len = buffer.len() as DWORD;
    unsafe {
        GetLogicalDriveStringsA(buffer_len, buffer_ptr);
    }
    let slice_u8 = &buffer[..];
    match str::from_utf8(slice_u8) {
        Ok(s) => {
            // 转换成功，现在s是一个&str
            // 将&str转换为String，并放入Vec<String>中
            let vec_string: Vec<String> = vec![s.to_string()];
            return vec_string;
        }
        Err(e) => {
            // 转换失败，e包含了错误信息
            eprintln!("Invalid UTF-8 sequence: {}", e);
            return Vec::new();
        }
    }

    /*let mut drive_strings = Vec::new();
    let mut current_string = Vec::new();
    let buffer_end = (buffer_len - 1) as usize;
    for (i, byte) in buffer.iter().enumerate() {
        if *byte == 0 {
            // 找到了一个null字符，当前字符串结束
            drive_strings.push(current_string);
            current_string.clear();
        } else {
            // 否则，将字符添加到当前字符串中
            current_string.push(*byte as char);
        }

        // 如果我们到达缓冲区的末尾，退出循环
        if i == buffer_end {
            break;
        }
    }
    let vec_of_string: Vec<String> = drive_strings.into_iter().map(|chars| {
        chars.into_iter().collect::<String>()
    }).collect();
    return vec_of_string;*/
}
